<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        a {
            text-decoration: none;
        }
        .container {
            width: 600px;
            height: 1200px;
            background-color: pink;
            margin: 0 auto;
        }
        .backTop {
            position: fixed;
            display: none;
            left: 50%;
            transform: translateX(300px);
            bottom: 60px;
            width: 50px;
            height: 50px;
            background-color: skyblue;
            text-align: center;
            line-height: 50px;
            color: white;
            font-weight: bold;
        }
    </style>

    <script>
        window.addEventListener('DOMContentLoaded', function() {
            // 获取元素
            const container = document.querySelector('.container');
            const backTop = document.querySelector('.backTop');

            // 滚动监听
            window.addEventListener('scroll', function(e) {
                let topY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
                if (topY > 100) backTop.style.display = 'block';
                else backTop.style.display = 'none';
            })

            // 返回顶部
            backTop.onclick = function() {
                let timer = setInterval(function() {
                    let topY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
                    let step = Math.ceil(topY / 10);
                    if (topY == 0) {
                        clearInterval(timer);
                    }
                    window.scroll(0, topY - step);
                }, 15);
            }
        });
    </script>
</head>
<body>
    <div class="container"></div>
    <a class="backTop" href="javascript:;">^</a>
</body>
</html>